package com.jic.veteran.portal.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.veteran.common.exception.ApiException;
import com.jic.veteran.mapper.AlumniSchoolAddresslistMapper;
import com.jic.veteran.model.AlumniSchoolAddresslist;
import com.jic.veteran.model.AlumniSchoolAddresslistExample;
import com.jic.veteran.portal.component.VeteranSecurityContext;
import com.jic.veteran.portal.dao.AlumniStudnetDao;
import com.jic.veteran.portal.domain.AlumniParamDto;
import com.jic.veteran.portal.domain.AlumniStudentInfoChangeParam;
import com.jic.veteran.portal.domain.AlumniStudentParam;
import com.jic.veteran.portal.service.AlumniStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 获取学校相关接口
 * Created by mars on 2021/11/10.
 */
@Slf4j
@Service
public class AlumniStudentServiceImpl implements AlumniStudentService {

    @Autowired
    private AlumniSchoolAddresslistMapper alumniSchoolAddresslistMapper;
    @Autowired
    private AlumniStudnetDao alumniStudnetDao;

    /**
     * 获取全部学生列表.
     */
    @Override
    public List<AlumniSchoolAddresslist> getStudnetsList(AlumniStudentParam alumniStudentParam) {

        List<AlumniSchoolAddresslist> alumniSchoolAddresslist;
        alumniSchoolAddresslist = alumniStudnetDao.getStudnetsList(alumniStudentParam);
        return alumniSchoolAddresslist;

    }

    /**
     * 获取全部学生列表分页.
     */
    @Override
    public List<AlumniSchoolAddresslist> getStudnetsListPage(AlumniStudentParam alumniStudentParam,
                                                             Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);


        List<AlumniSchoolAddresslist> alumniSchoolAddresslist;
        alumniSchoolAddresslist = alumniStudnetDao.getStudnetsList(alumniStudentParam);

        return alumniSchoolAddresslist;
    }

    /**
     * 获取学生详细信息.
     */
    @Override
    public AlumniSchoolAddresslist getStudnetDetail(Long id) {
        AlumniSchoolAddresslistExample alumniSchoolAddresslistExample = new AlumniSchoolAddresslistExample();
        AlumniSchoolAddresslistExample.Criteria criteria = alumniSchoolAddresslistExample.createCriteria();
        criteria.andStatusEqualTo(1)
                .andIdEqualTo(id);
        AlumniSchoolAddresslist alumniSchoolAddresslist = alumniSchoolAddresslistMapper.selectByExampleWithBLOBs(alumniSchoolAddresslistExample).get(0);
        if (alumniSchoolAddresslist.getId() == null ) {
            throw new ApiException("未找到详情信息");
        }
        return alumniSchoolAddresslist;
    }


    /**
     * 小程序修改学生信息.
     */
    @Override
    public int changeInfo(Integer id, AlumniStudentInfoChangeParam alumniStudentInfoChangeParam) {

        return alumniStudnetDao.changeInfo(id,alumniStudentInfoChangeParam,
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }

    @Override
    public List<String> searchParam(Integer type) {
        return alumniStudnetDao.searchParam(type);
    }

    @Override
    public int changeAvatar(Integer id, String avatarUrl) {
        return alumniStudnetDao.changeAvatar(id,avatarUrl,
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }
}
